﻿using Common;
using KBZ.Common;
using ServiceLibrary.DtoModel;
using ServiceLibrary.DtoModel.DeskApp;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace ServiceLibrary.Implements
{
    public class sysMenutypeService :  BaseServer<sysMenuType>
    {
        /// <summary>
        /// 查询所有菜单类型的方法
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<sysMenuType>>> QueryMenuTypes(ParmString parms)
        {
            var res = new ApiResult<List<sysMenuType>>() { statusCode = (int)ApiEnum.Error };
            try
            {
                DataTable _TB = null;
                var sql = new StringBuilder(" select * from MenuType ");
                if (!string.IsNullOrEmpty(parms.parm))
                {
                    var _parms = new List<SugarParameter>() { new SugarParameter("@rName", parms.parm) };
                    sql.Append(" where rName=@rName ");
                    _TB = Db.Ado.GetDataTable(sql.ToString(), _parms);
                }
                else
                {
                    _TB = Db.Ado.GetDataTable(sql.ToString());
                }
                List<sysMenuType> _datas = new ModelHandler<sysMenuType>().FillModel(_TB);
                res.data = _datas;
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.message = ex.Message;
                res.success = false;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<Page<sysMenuType>>> GetPagesAsync(PageParm parm)
        {
            var res = new ApiResult<Page<sysMenuType>>();
            try
            {
                res.data = await Db.Queryable<sysMenuType>()
                                   .WhereIF(!string.IsNullOrEmpty(parm.key), m => m.mtName.Contains(parm.key))
                                   .OrderBy(m => m.createDate, OrderByType.Desc)
                                   .ToPageAsync(parm.page, parm.limit);
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (Exception ex)
            {
                res.statusCode = (int)ApiEnum.Error;
                Logger.Default.ProcessError((int)ApiEnum.Error, ex.Message);
            }
            return res;
        }

        /// <summary>
        /// 根据主键删除数据
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> DeleteAsync(string parm)
        {
            var list = Utils.StrToListString(parm);
            var isok = await Db.Deleteable<sysMenus>().Where(m => list.Contains(m.proGuid)).ExecuteCommandAsync();
            var res = new ApiResult<string>
            {
                success = isok > 0,
                statusCode = isok > 0 ? (int)ApiEnum.Status : (int)ApiEnum.Error,
                data = isok > 0 ? "1" : "0"
            };
            return res;
        }
    }
}
